package thoven.achievement.net.ideapluginachievement.ui

import com.intellij.openapi.diagnostic.logger
import com.intellij.openapi.project.Project
import com.intellij.openapi.ui.DialogWrapper
import com.intellij.ui.JBColor
import com.intellij.ui.components.JBLabel
import com.intellij.ui.components.JBTabbedPane
import com.intellij.util.ui.JBUI
import thoven.achievement.net.ideapluginachievement.service.ReportGeneratorService
import thoven.achievement.net.ideapluginachievement.service.ReportType
import java.awt.BorderLayout
import java.awt.Dimension
import java.awt.Font
import javax.swing.*

/**
 * 报告对话框
 */
class ReportDialog(
    private val project: Project,
    private val initialReportType: ReportType = ReportType.DAILY
) : DialogWrapper(project) {
    
    private val LOG = logger<ReportDialog>()
    private val reportGeneratorService = ReportGeneratorService.getInstance(project)
    private val tabbedPane = JBTabbedPane()
    
    init {
        title = "编码报告"
        LOG.info("初始化编码报告对话框，类型: $initialReportType")
        init()
        
        // 设置初始选中的标签页
        when (initialReportType) {
            ReportType.DAILY -> tabbedPane.selectedIndex = 0
            ReportType.WEEKLY -> tabbedPane.selectedIndex = 1
            ReportType.MONTHLY -> tabbedPane.selectedIndex = 2
        }
    }
    
    override fun createCenterPanel(): JComponent {
        LOG.info("创建报告对话框中心面板")
        val panel = JPanel(BorderLayout())
        panel.preferredSize = Dimension(800, 600)
        panel.border = JBUI.Borders.empty(10)
        
        try {
            // 创建标签页
            tabbedPane.addTab("日报", createReportPanel(ReportType.DAILY))
            tabbedPane.addTab("周报", createReportPanel(ReportType.WEEKLY))
            tabbedPane.addTab("月报", createReportPanel(ReportType.MONTHLY))
            
            panel.add(tabbedPane, BorderLayout.CENTER)
        } catch (e: Exception) {
            LOG.error("创建报告标签页时出错", e)
            e.printStackTrace()
            
            // 显示错误信息
            val errorPanel = JPanel(BorderLayout())
            val errorMessage = e.message ?: e.javaClass.simpleName
            val errorLabel = JBLabel("<html>生成报告时出错: $errorMessage<br/><br/>错误详情: ${e.toString()}</html>")
            errorLabel.foreground = JBColor.RED
            errorPanel.add(errorLabel, BorderLayout.CENTER)
            panel.add(errorPanel, BorderLayout.CENTER)
        }
        
        return panel
    }
    
    /**
     * 创建报告面板
     */
    private fun createReportPanel(reportType: ReportType): JComponent {
        LOG.info("创建${reportType}报告面板")
        val panel = JPanel(BorderLayout())
        panel.border = JBUI.Borders.empty(10)
        
        try {
            // 生成报告内容
            val reportContent = reportGeneratorService.generateReport(reportType)
            LOG.info("生成报告内容成功，类型: $reportType, 长度: ${reportContent.length}")
            
            // 创建带有HTML内容的JEditorPane
            val editorPane = JEditorPane()
            editorPane.contentType = "text/html"
            editorPane.isEditable = false
            editorPane.background = JBColor.background()
            editorPane.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, true)
            editorPane.font = UIManager.getFont("Label.font")
            
            // 在创建后再设置内容，避免构造时的HTML解析问题
            editorPane.text = reportContent
            
            // 添加滚动条
            val scrollPane = JScrollPane(editorPane)
            scrollPane.border = BorderFactory.createEmptyBorder()
            
            panel.add(scrollPane, BorderLayout.CENTER)
        } catch (e: Exception) {
            LOG.error("创建报告面板时出错，报告类型: $reportType", e)
            e.printStackTrace()
            
            // 显示详细错误信息
            val errorMessage = e.message ?: e.javaClass.simpleName
            val errorText = """
                <html>
                <div style='padding: 20px; color: red;'>
                    <h3>生成报告时出错</h3>
                    <p>错误类型: ${e.javaClass.simpleName}</p>
                    <p>错误信息: $errorMessage</p>
                    <p>报告类型: $reportType</p>
                    <br/>
                    <p style='color: gray;'>请检查日志获取更多详细信息</p>
                </div>
                </html>
            """.trimIndent()
            
            val errorLabel = JBLabel(errorText)
            panel.add(errorLabel, BorderLayout.CENTER)
        }
        
        return panel
    }
    
    /**
     * 创建底部按钮面板
     */
    override fun createSouthPanel(): JComponent {
        val panel = JPanel(BorderLayout())
        panel.border = JBUI.Borders.empty(10, 0, 0, 0)
        
        val buttonsPanel = super.createSouthPanel()
        panel.add(buttonsPanel, BorderLayout.EAST)
        
        return panel
    }
} 